package com.example.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Order(1)
@Slf4j
@Aspect
@Component
public class MyAspect {
    @Before("com.example.aspect.Timeaspect.put()")
    public void BeforeTime() {
        log.info("Test Aop BeforeTime...");
    }

    @Around("execution(* com.example.controller..*.*(..))")
    public Object recordTime(ProceedingJoinPoint joinPoint) throws Throwable {
        // 获取目标对象的类名
        String className = joinPoint.getTarget().getClass().getName();
        log.info("目标对象的类名:{}",className);
        // 获取目标方法的方法名
        String methodName = joinPoint.getSignature().getName();
        log.info("目标方法的方法名:{}",methodName);

        // 获取目标方法运行时传入的参数
        Object[] args = joinPoint.getArgs();
        log.info("目标方法运行时传入的参数:{}",args);

        // 获取目标方法运行后的返回值
        Object result = joinPoint.proceed();// 调用原始操作
        log.info("目标方法运行后的返回值：{}",result);
        return result;
    }

    @After("com.example.aspect.Timeaspect.put()")
    public void AfterTime() {
        log.info("Test Aop AfterTime...");
    }

    @AfterReturning("com.example.aspect.Timeaspect.put()")
    public void AfterReturningTime() {
        log.info("Test Aop AfterReturningTime...");
    }

    @AfterThrowing("")
    public void AfterThrowingTime() {
        log.info("Test Aop AfterThrowingTime...");
    }
}
